package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageBean;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingPlanPageQueryDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {

    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    /**
     * 获取所有护理计划
     *
     * @return
     */
    @Override
    public List<NursingPlanVo> list() {
        return nursingPlanMapper.getAll();
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlanDto
     */
    @Transactional
    @Override
    public void addNursingPlan(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        nursingPlan.setStatus(1);
        nursingPlanMapper.insert(nursingPlan);
        nursingPlanDto.getProjectPlans().forEach(projectPlan -> projectPlan.setPlanId(nursingPlan.getId()));
        List<NursingProjectPlan> list = nursingPlanDto.getProjectPlans().stream()
                .map(nursingProjectPlanDto -> BeanUtil.copyProperties(nursingProjectPlanDto, NursingProjectPlan.class))
                .collect(Collectors.toList());
        nursingProjectPlanMapper.insertList(list);
    }

    /**
     * 条件分页查询护理计划
     *
     * @param dto
     * @return
     */
    @Override
    public PageBean<NursingPlanVo> searchNursingPlan(NursingPlanPageQueryDto dto) {
        // 1.开始分页
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        Page<NursingPlanVo> page = nursingPlanMapper.pageQuery(dto);
        // 2.给分页结果封装关联数据
        List<NursingPlanVo> nursingPlanVoList = page.getResult().stream().map(nursingPlanVo -> {
            List<NursingLevel> nursingLevelList = nursingLevelMapper.getByPlanId(nursingPlanVo.getId());
            long count = nursingLevelList.stream().count();
            nursingPlanVo.setCount(count);
            return nursingPlanVo;
        }).collect(Collectors.toList());
        return new PageBean<>(page.getTotal(), page.getPageSize(),(long)page.getPages(), page.getPageNum(), nursingPlanVoList);
    }

    /**
     * 根据id查询护理计划
     *
     * @param id
     * @return
     */
    @Override
    public NursingPlanVo getNursingPlanById(Long id) {
        // 1.查询id对应的护理计划
        NursingPlan nursingPlan = nursingPlanMapper.getById(id);
        // 2.查询护理计划对应的所有护理项目
        List<NursingProjectPlanVo> projectPlanVos = nursingProjectPlanMapper.getByPlanId(id);
        NursingPlanVo nursingPlanVo = BeanUtil.copyProperties(nursingPlan, NursingPlanVo.class);
        nursingPlanVo.setProjectPlans(projectPlanVos);
        return nursingPlanVo;
    }

    /**
     * 修改护理计划
     *
     * @param nursingPlanDto
     */
    @Transactional
    @Override
    public void updateNursingPlan(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        nursingPlanMapper.updateById(nursingPlan);
        nursingProjectPlanMapper.deleteByPlanId(nursingPlan.getId());
        nursingPlanDto.getProjectPlans().forEach(projectPlan -> projectPlan.setPlanId(nursingPlan.getId()));
        List<NursingProjectPlan> list = nursingPlanDto.getProjectPlans().stream()
                .map(nursingProjectPlanDto -> BeanUtil.copyProperties(nursingProjectPlanDto, NursingProjectPlan.class))
                .collect(Collectors.toList());
        nursingProjectPlanMapper.insertList(list);
    }

    /**
     * 删除护理计划
     * @param id
     */
    @Transactional
    @Override
    public void deleteNursingPlan(Long id) {
        nursingPlanMapper.deleteById(id);
        nursingProjectPlanMapper.deleteByPlanId(id);
    }

    /**
     * 编辑状态
     *
     * @param id
     * @param status
     */
    @Override
    public void enableOrDisableNursingPlan(Long id, Integer status) {
        NursingPlan nursingPlan = nursingPlanMapper.getById(id);
        nursingPlan.setStatus(status);
        nursingPlanMapper.updateById(nursingPlan);
    }
}
